package ecom.beans;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "product")
@NamedQueries({
    @NamedQuery(name = "ProductBean.findAll", query = "SELECT p FROM ProductBean p"),
    @NamedQuery(name = "ProductBean.findByIdProduct", query = "SELECT p FROM ProductBean p WHERE p.idProduct = :idProduct"),
    @NamedQuery(name = "ProductBean.findByNameOrDescription", query = "SELECT p FROM ProductBean p WHERE p.name LIKE :name "
    + "OR p.description LIKE :description"),
    @NamedQuery(name = "ProductBean.findByIdProductcategory", query = "SELECT p FROM ProductBean p "
    + "WHERE p.idProductCategory.idProductcategory = :idProductcategory"),
    @NamedQuery(name = "ProductBean.findByIdProductstore", query = "SELECT p FROM ProductBean p "
    + "INNER JOIN p.stockBeanSet s WHERE s.stockBeanPK.idProductStore = :idProductstore"),
    @NamedQuery(name = "ProductBean.findByPriceInterval", query = "SELECT DISTINCT p FROM ProductBean p "
    + "INNER JOIN p.stockBeanSet s "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.price >= :firstPrice and s.price <= :secondPrice "),
    @NamedQuery(name = "ProductBean.findByMaximumPrice", query = "SELECT DISTINCT p FROM ProductBean p "
    + "INNER JOIN p.stockBeanSet s "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.price <= :maxPrice")
})
public class ProductBean implements Serializable, ProductRemote {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_product", nullable = false)
    private Long idProduct;
    @Basic(optional = false)
    @Column(name = "name", nullable = false, length = 50)
    private String name;
    @Column(name = "description", length = 400)
    private String description;
    @Column(name = "url_photo", length = 100)
    private String urlPhoto;
    @JoinColumn(name = "id_product_category", referencedColumnName = "idProductcategory", nullable = false)
    @ManyToOne(optional = false)
    private ProductcategoryBean idProductCategory;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productBean")
    private Set<StockBean> stockBeanSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productBean")
    private Set<PurchaseBean> purchaseBeanSet;

    public ProductBean() {
    }

    public ProductBean(String name, ProductcategoryBean idProductCategory) {
        this.name = name;
        this.idProductCategory = idProductCategory;

    }

    public ProductBean(String name, String description, String urlPhoto, ProductcategoryBean idProductCategory) {
        this.name = name;
        this.description = description;
        this.urlPhoto = urlPhoto;
        this.idProductCategory = idProductCategory;
    }

    @Override
    public Long getIdProduct() {
        return idProduct;
    }

    public void setIdProduct(Long idProduct) {
        this.idProduct = idProduct;
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrlPhoto() {
        return urlPhoto;
    }

    public void setUrlPhoto(String urlPhoto) {
        this.urlPhoto = urlPhoto;
    }

    public ProductcategoryBean getIdProductCategory() {
        return idProductCategory;
    }

    public void setIdProductCategory(ProductcategoryBean idProductCategory) {
        this.idProductCategory = idProductCategory;
    }

    public Set<StockBean> getStockBeanSet() {
        return stockBeanSet;
    }

    public void setStockBeanSet(Set<StockBean> stockBeanSet) {
        this.stockBeanSet = stockBeanSet;
    }

    public Set<PurchaseBean> getPurchaseBeanSet() {
        return purchaseBeanSet;
    }

    public void setPurchaseBeanSet(Set<PurchaseBean> purchaseBeanSet) {
        this.purchaseBeanSet = purchaseBeanSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idProduct != null ? idProduct.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ProductBean)) {
            return false;
        }
        ProductBean other = (ProductBean) object;
        if ((this.idProduct == null && other.idProduct != null) || (this.idProduct != null && !this.idProduct.equals(other.idProduct))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("[idProduct=").append(this.idProduct);
        result.append(",name=").append(this.name);
        result.append(",description=").append(this.description);
        result.append(",urlPhoto=").append(this.urlPhoto);
        result.append(",idProductcategory=").append(this.idProductCategory.getIdProductcategory()).append("]");
        return result.toString();
    }
}
